<?php
// 前台学校食堂管理模块
class NhAction extends CommonAction {
	function _filter(&$map){
		/*if('POST'==$_SERVER['REQUEST_METHOD']||$_SESSION['searchND1']>0){
			$map['nd'] = $_POST['nd']?array('eq',$_POST['nd']):array('eq',$_SESSION['searchND1']);
			$_SESSION['searchND1'] = $_POST['nd'];
		}else{
			$map['nd'] = array('eq',date('Y'));
			$_SESSION['searchND1'] = date('Y');
		}*/
		if('POST'==$_SERVER['REQUEST_METHOD']){
			$map['nd'] = $_POST['nd']?array('eq',$_POST['nd']):array('eq',$_SESSION['searchND1']);
			$_SESSION['searchND1'] = $_POST['nd'];
		}elseif($_SESSION['searchND1']>0){
			$map['nd'] = array('eq',$_SESSION['searchND1']);
		}else{
			$map['nd'] = array('eq',date('Y'));
			$_SESSION['searchND1'] = date('Y');
		}		
		
		$map['id'] = array('egt',1);
		$map['sName'] = array('like',"%".$_POST['keyword']."%");
		if(0!=$_SESSION['dwid'])$map['dwid'] = array('eq',$_SESSION['dwid']);
	}
	
	public function index() {
        //列表过滤器，生成查询Map对象
        $map = $this->_search('vnh');
        if (method_exists($this, '_filter')) {
            $this->_filter($map);
        }
        $name = 'vnh';
        $model = D($name);
        if (!empty($model)) {
            $this->_list($model, $map);
        }
        $this->display();
        return;
    }
	//更新页面显示
	function edit() {
        $model = D('vnh');
        $id = $_REQUEST ['id'];
		$id = 0==$_SESSION['dwid']?$_REQUEST['id']:$_SESSION['dwid'];
        $vo = $model->where('dwid='.$id.' and nd='.$_SESSION['searchND1'])->find();
        $this->assign('vo', $vo);
        $this->display();
    }
	//不动产更新操作
	function update(){
		$model = D('Nh');	
		$dwid = 0==$_SESSION['dwid']?$_POST['dwid']:$_SESSION['dwid'];
        if (false !== $model->create()) {
			$list = $model->where('dwid='.$dwid.' and nd='.$_SESSION['searchND1'])->save();
			if(false!==$list){
				//成功提示
				$this->assign('jumpUrl', Cookie::get('_currentUrl_'));
				$this->success('编辑成功!',true);
			}else{
				$this->error('编辑失败!',true);
			}
        } else {
            //错误提示
           $this->error($model->getError(),true);
        }
	}
	public function forbid() {
        $model = D('Nh');
        $id = $_REQUEST ['id'];
        $condition = array('dwid' => array('in', $id));
		
		$list = $model->forbid($condition);
        if ($list !== false) {
            $this->assign("jumpUrl", $this->getReturnUrl());
            $this->success('状态禁用成功');
        } else {
            $this->error('状态禁用失败！');
        }
        
    }
	function resume() {
        $model = D('Nh');
        $id = $_GET ['id'];
        $condition = array('dwid' => array('in', $id));
		
        if (false !== $model->resume($condition)) {
            $this->assign("jumpUrl", $this->getReturnUrl());
            $this->success('状态恢复成功！');
        } else {
            $this->error('状态恢复失败！');
        }
    }
	
	public function lockAll(){
		$name = $this->getActionName();
        $model = D($name);
		if (!empty($model)) {
			if (false !== $model->where("dwid>0 and nd=".$_SESSION['searchND1'])->setField('status',$_POST['status'])) {
				//echo $model->getlastsql();
				$this->success('锁定成功！',true);
			} else {
				$this->error('锁定失败！',false);
			}
		} else {
			$this->error('非法操作',false);
		}
        $this->forward();	
	}
	
	function addnh(){
		$nd1 = $_GET['nd1'];//年度
		$nd2 = $_GET['nd2'];//是否锁定以往数据
		$nd3 = $_GET['nd3'];//是否复制去年数据
		
		if(!$nd1>2000){
			$nd1	= date('Y');	
		}
		
		$nd = D('Nh');
		
		if($nd->where('nd='.$nd1)->select()){
			$this->error('年度信息重复！添加能源消耗信息失败！');
			return;
		}
		if($nd2){
			$nd->where('nd!='.$nd1)->setField('status',0);	
		}	
		if($nd3){
			$sql = "INSERT INTO think_nh(nd,dwid,ggjgsl,jzmj,gnmj,glmj,ynrs,bzrs,clsl,clbzsl,gwycsl,qtytclsl,qycl,cycl,dxhl,dfy,sxhl,sfy,ymxhzl,ymzfy,ymcnymxhl,ymcnymfy,ymqtytmxhl,ymqtytmfy,tyqxhl,tyqfy,yhsyqxhl,yhsyqfy,rgmqxhl,rgmqfy,qyxhzl,qyzfy,qygwycxhl,qygwycfy,qyqtytclxhl,qyqtytclfy,qyqtxhl,qyqtfy,cyxhzl,cyzfy,cygwycxhl,cygwycfy,cyqtytclxhl,cyqtytclfy,cyqtxhl,cyqtfy,myxhl,myfy,rlxhl,rlfy,qtnyxhl,qtnyfy) SELECT ".$nd1.",dwid,ggjgsl,jzmj,gnmj,glmj,ynrs,bzrs,clsl,clbzsl,gwycsl,qtytclsl,qycl,cycl,dxhl,dfy,sxhl,sfy,ymxhzl,ymzfy,ymcnymxhl,ymcnymfy,ymqtytmxhl,ymqtytmfy,tyqxhl,tyqfy,yhsyqxhl,yhsyqfy,rgmqxhl,rgmqfy,qyxhzl,qyzfy,qygwycxhl,qygwycfy,qyqtytclxhl,qyqtytclfy,qyqtxhl,qyqtfy,cyxhzl,cyzfy,cygwycxhl,cygwycfy,cyqtytclxhl,cyqtytclfy,cyqtxhl,cyqtfy,myxhl,myfy,rlxhl,rlfy,qtnyxhl,qtnyfy FROM think_nh WHERE nd=".($nd1-1);//date('Y');
		}else{
			$sql = "INSERT INTO think_nh(nd,dwid) SELECT ".$nd1.",dwid FROM think_nh WHERE nd=".date('Y');	
		}
		if (false !== $nd->execute($sql)) {
            $this->assign("jumpUrl", $this->getReturnUrl());
            $this->success('添加能源消耗信息成功！');
        } else {
            $this->error('添加能源消耗信息失败！');
        }
	}
}
?>